package com.mxnet.chongjin.supoman.fragment;


import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.content.res.Configuration;
import android.os.Bundle;
import android.os.CountDownTimer;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentTransaction;
import android.text.TextUtils;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.android.tu.loadingdialog.LoadingDailog;
import com.mxnet.chongjin.supoman.R;
import com.mxnet.chongjin.supoman.activity.AppointTimeActivity;
import com.mxnet.chongjin.supoman.config.Api;
import com.mxnet.chongjin.supoman.style.MenuViewItem;
import com.mxnet.chongjin.supoman.utils.HttpUtils;
import com.mxnet.chongjin.supoman.utils.ICallBack;
import com.mxnet.chongjin.supoman.utils.LoggerUtils;
import com.mxnet.chongjin.supoman.utils.StringUtils;
import com.mxnet.chongjin.supoman.utils.TCPUtils;
import com.mxnet.chongjin.supoman.utils.ToastUtils;
import com.zhy.http.okhttp.OkHttpUtils;
import com.zhy.http.okhttp.callback.StringCallback;

import org.json.JSONObject;

import java.util.Timer;
import java.util.TimerTask;

import okhttp3.Call;


/**
 * A simple {@link Fragment} subclass.
 */
public class MachineWorkFragment extends Fragment implements View.OnClickListener {
    private Context mContext;
    private View view;
    private LinearLayout mLayout1, mLayout2, mLayout3, mLayout4;
    private MenuViewItem mMenuViewItemUp, mMenuViewItemDown, mMenuViewItemLeft, mMenuViewItemRight, mMenuViewItemCenter;
    private TextView mTextView1;
    private SharedPreferences mPreferences;
    private SharedPreferences.Editor mEditor;
    private LayoutInflater mInflater;
    private int sequenceNum;
    private ViewGroup mContainer;
    private Bundle mSaveInstanceState;
    private String MAC;
    private final boolean isInvolute = false;
    private Timer mTimer;

    public MachineWorkFragment() {
        // Required empty public constructor
    }

    @Override
    public void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
    }

    @Override
    public void onSaveInstanceState(Bundle outState) {
        super.onSaveInstanceState(outState);
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        mInflater = inflater;
        mContainer = container;
        mSaveInstanceState = savedInstanceState;
        // Inflate the layout for this fragment
        if (view == null) {
            if (getResources().getConfiguration().orientation == Configuration.ORIENTATION_LANDSCAPE) {
                Log.i("***orientation", "land1");
                view = inflater.inflate(R.layout.fragment_machine_work_land, container, false);
            } else {
                Log.i("***orientation", "port1");
                view = inflater.inflate(R.layout.fragment_machine_work, container, false);
            }
        } else {
            if (getResources().getConfiguration().orientation == Configuration.ORIENTATION_LANDSCAPE) {
                Log.i("***orientation", "land2");

                //                container.removeView(view);
                view = inflater.inflate(R.layout.fragment_machine_work_land, container, false);
            } else {
                Log.i("***orientation", "port2");
                view = inflater.inflate(R.layout.fragment_machine_work, container, false);
            }

        }

        mContext = getContext();
        mPreferences = mContext.getSharedPreferences("supoman", Context.MODE_PRIVATE);
        mEditor = mPreferences.edit();
        MAC = mPreferences.getString("MAC", "E05A2834FFFF");
        initView(view);

        mTimer = new Timer();
        mTimer.schedule(new TimerTask() {
            @Override
            public void run() {
                setStatusControl();
            }
        }, 5000, 5000);

        return view;
    }

    public void initView(View view) {
        mLayout1 = view.findViewById(R.id.ll_machine_work_1);
        mLayout1.setOnClickListener(this);

        mLayout2 = view.findViewById(R.id.ll_machine_work_2);
        mLayout2.setOnClickListener(this);

        mLayout3 = view.findViewById(R.id.ll_machine_work_3);
        mLayout3.setOnClickListener(this);

        mLayout4 = view.findViewById(R.id.ll_machine_work_4);
        mLayout4.setOnClickListener(this);

        mMenuViewItemUp = view.findViewById(R.id.machine_btn_forward);
        mMenuViewItemUp.setOnClickListener(this);

        mMenuViewItemDown = view.findViewById(R.id.machine_btn_backward);
        mMenuViewItemDown.setOnClickListener(this);

        mMenuViewItemLeft = view.findViewById(R.id.machine_btn_left);
        mMenuViewItemLeft.setOnClickListener(this);

        mMenuViewItemRight = view.findViewById(R.id.machine_btn_right);
        mMenuViewItemRight.setOnClickListener(this);

        mMenuViewItemCenter = view.findViewById(R.id.machine_btn_run);
        mMenuViewItemCenter.setOnClickListener(this);

        mTextView1 = view.findViewById(R.id.tv_machine_work_mode);
    }

    @Override
    public void onResume() {
        super.onResume();
        setStatusControl();
    }

    boolean isRunning = false;

    @Override
    public void onClick(View v) {
        LoadingDailog.Builder loadBuilder = new LoadingDailog.Builder(mContext)
                .setMessage(getResources().getString(R.string.excuting))
                .setCancelable(true)
                .setCancelOutside(true);
        final LoadingDailog dialog  = loadBuilder.create();
        boolean isCmd=false;
        String cmd="";
        sequenceNum = mPreferences.getInt("sequenceNum", 1);
        String seq = getSeq(Integer.toHexString(sequenceNum));
        String startPwd = mPreferences.getString("startPwd", "FFFFFFFF");
        switch (v.getId()) {
            case R.id.ll_machine_work_1:
                isCmd=true;
                cmd=Api.HEAD + MAC + Api.MY_MAC + startPwd + seq + Api.AUTO;
                dialog.setCancelable(false);   //是否能取消弹窗
                dialog.show();
                break;
            case R.id.ll_machine_work_2:
                Intent intent = new Intent();
                intent.setClass(mContext, AppointTimeActivity.class);
                mContext.startActivity(intent);
                break;
            case R.id.ll_machine_work_3:
                isCmd=true;
                cmd=Api.HEAD + MAC + Api.MY_MAC + startPwd + seq + Api.CHARGE;
                dialog.setCancelable(false);   //是否能取消弹窗
                dialog.show();
                break;
            case R.id.ll_machine_work_4:
                if (!isInvolute) {
                    isCmd=true;
                    cmd=Api.HEAD + MAC + Api.MY_MAC + startPwd + seq + Api.INVOLUTE;
                    dialog.setCancelable(false);   //是否能取消弹窗
                    dialog.show();
                } else {
                    isCmd=true;
                    cmd=Api.HEAD + MAC + Api.MY_MAC + startPwd + seq + Api.INVOLUTE_STOP;
                    dialog.setCancelable(false);   //是否能取消弹窗
                    dialog.show();
                }
                break;
            case R.id.machine_btn_forward:
                isCmd=true;
                cmd=Api.HEAD + MAC + Api.MY_MAC + startPwd + seq + Api.FORWARD;
                dialog.setCancelable(false);   //是否能取消弹窗
                dialog.show();
                break;
            case R.id.machine_btn_backward:
                isCmd=true;
                cmd=Api.HEAD + MAC + Api.MY_MAC + startPwd + seq + Api.BACKWARD;
                dialog.setCancelable(false);   //是否能取消弹窗
                dialog.show();
                break;
            case R.id.machine_btn_left:
                isCmd=true;
                cmd=Api.HEAD + MAC + Api.MY_MAC + startPwd + seq + Api.LEFT;
                dialog.setCancelable(false);   //是否能取消弹窗
                dialog.show();
                break;
            case R.id.machine_btn_right:
                isCmd=true;
                cmd=Api.HEAD + MAC + Api.MY_MAC + startPwd + seq + Api.RIGHT;
                dialog.setCancelable(false);   //是否能取消弹窗
                dialog.show();
                break;
            case R.id.machine_btn_run:
                if (isRunning) {
                    isCmd=true;
                    cmd=Api.HEAD + MAC + Api.MY_MAC + startPwd + seq + Api.STOP;
                    dialog.setCancelable(false);   //是否能取消弹窗
                    dialog.show();

                } else {
                    isCmd=true;
                    cmd=Api.HEAD + MAC + Api.MY_MAC + startPwd + seq + Api.START;
                    dialog.setCancelable(false);   //是否能取消弹窗
                    dialog.show();
                }
                break;
        }

        if(isCmd)
        {
            final String msg=cmd;
            final int vID=v.getId();
            new Thread(new Runnable() {
                @Override
                public void run() {
                    sequenceNum = mPreferences.getInt("sequenceNum", 1);
                    String seq = getSeq(Integer.toHexString(sequenceNum));
                    HttpUtils.controlCmd(msg, MAC, seq, new ICallBack() {
                        @Override
                        public void onError(Exception exception, int id) {
                            ToastUtils.showToast(mContext, getResources().getString(R.string.net_fault));
                            dialog.dismiss();
                        }

                        @Override
                        public void onSuccess(String seq) {
                            if(vID==R.id.machine_btn_run)
                            {
                                isRunning=!isRunning;
                                if(isRunning)
                                {
                                    mMenuViewItemCenter.setBackgroundResource(R.mipmap.machine_btn_pause);
                                }
                                else
                                {
                                    mMenuViewItemCenter.setBackgroundResource(R.mipmap.machine_btn_run);
                                }
                            }

                            setStatusControl();
                            ToastUtils.showToast(mContext, getResources().getString(R.string.control_success));
                            dialog.dismiss();
                        }

                        @Override
                        public void onFail(String seq, String msg) {
                            ToastUtils.showToast(mContext, getResources().getString(R.string.control_failed)+" "+msg);
                            dialog.dismiss();
                        }

                        @Override
                        public void onOverTime() {
                            ToastUtils.showToast(mContext, getResources().getString(R.string.no_response));
                            dialog.dismiss();
                        }
                    });
                    sequenceNum++;
                    mEditor.putInt("sequenceNum",  sequenceNum  % 65535);
                    mEditor.commit();

                }
            }).start();
        }
    }

    @Override
    public void onConfigurationChanged(Configuration newConfig) {
        //        super.onConfigurationChanged(newConfig);
        //        onCreateView(mInflater, mContainer, mSaveInstanceState);
        if (newConfig.orientation == Configuration.ORIENTATION_LANDSCAPE) {
            Log.i("*****onConfigChange", "onConfigurationChanged");
            onCreateView(mInflater, mContainer, mSaveInstanceState);
        } else {
            //onCreateView(mInflater, mContainer, mSaveInstanceState);
        }
        super.onConfigurationChanged(newConfig);
    }

    //    @Override
    //    public void onHiddenChanged(boolean hidden) {
    //        getActivity().setRequestedOrientation(//通过程序改变屏幕显示的方向
    //                hidden ? ActivityInfo.SCREEN_ORIENTATION_NOSENSOR
    //                        : ActivityInfo.SCREEN_ORIENTATION_SENSOR);
    //    }

    @Override
    public void onDestroy() {
        mTimer.cancel();
        super.onDestroy();
    }

    public String getSeq(String seq) {
        if (seq.length() == 1) {
            seq = "000" + seq;
        } else if (seq.length() == 2) {
            seq = "00" + seq;
        } else if (seq.length() == 3) {
            seq = "0" + seq;
        }
        return seq;
    }

    private void controlStatus(String cmd)
    {
        switch (cmd) {
            case "020104":
                // 空闲状态
                getActivity().runOnUiThread(new Runnable() {
                    @Override
                    public void run() {
                        if(mTextView1!=null)
                            mTextView1.setText(getResources().getString(R.string.idle));
                    }
                });

                mLayout1.setClickable(true);
                mLayout1.setAlpha(1.0f);
                mLayout2.setClickable(true);
                mLayout2.setAlpha(1.0f);
                mLayout3.setClickable(false);
                mLayout3.setAlpha(0.5f);
                mLayout4.setClickable(false);
                mLayout4.setAlpha(0.5f);

                mMenuViewItemUp.setClickable(false);
                mMenuViewItemUp.setAlpha(0.5f);
                mMenuViewItemDown.setClickable(false);
                mMenuViewItemDown.setAlpha(0.5f);
                mMenuViewItemLeft.setClickable(false);
                mMenuViewItemLeft.setAlpha(0.5f);
                mMenuViewItemRight.setClickable(false);
                mMenuViewItemRight.setAlpha(0.5f);
                mMenuViewItemCenter.setClickable(false);
                mMenuViewItemCenter.setAlpha(0.5f);
                break;
            case "020101":
                // 割草/工作
                getActivity().runOnUiThread(new Runnable() {
                    @Override
                    public void run() {
                        if(mTextView1!=null)
                            mTextView1.setText(getResources().getString(R.string.working));
                    }
                });

                mLayout1.setClickable(true);
                mLayout1.setAlpha(1.0f);
                mLayout2.setClickable(false);
                mLayout2.setAlpha(0.5f);
                mLayout3.setClickable(true);
                mLayout3.setAlpha(1.0f);
                mLayout4.setClickable(true);
                mLayout4.setAlpha(1.0f);

                mMenuViewItemUp.setClickable(true);
                mMenuViewItemUp.setAlpha(1.0f);
                mMenuViewItemDown.setClickable(true);
                mMenuViewItemDown.setAlpha(1.0f);
                mMenuViewItemLeft.setClickable(true);
                mMenuViewItemLeft.setAlpha(1.0f);
                mMenuViewItemRight.setClickable(true);
                mMenuViewItemRight.setAlpha(1.0f);
                mMenuViewItemCenter.setClickable(true);
                mMenuViewItemCenter.setAlpha(1.0f);
                break;
            case "020102":
                // 回航
                getActivity().runOnUiThread(new Runnable() {
                    @Override
                    public void run() {
                        if(mTextView1!=null)
                            mTextView1.setText(getResources().getString(R.string.backhome));
                    }
                });

                mLayout1.setClickable(true);
                mLayout1.setAlpha(1.0f);
                mLayout2.setClickable(true);
                mLayout2.setAlpha(1.0f);
                mLayout3.setClickable(true);
                mLayout3.setAlpha(1.0f);
                mLayout4.setClickable(true);
                mLayout4.setAlpha(1.0f);

                mMenuViewItemUp.setClickable(true);
                mMenuViewItemUp.setAlpha(1.0f);
                mMenuViewItemDown.setClickable(true);
                mMenuViewItemDown.setAlpha(1.0f);
                mMenuViewItemLeft.setClickable(true);
                mMenuViewItemLeft.setAlpha(1.0f);
                mMenuViewItemRight.setClickable(true);
                mMenuViewItemRight.setAlpha(1.0f);
                mMenuViewItemCenter.setClickable(true);
                mMenuViewItemCenter.setAlpha(1.0f);
                break;
            case "020103":
                // 充电
                getActivity().runOnUiThread(new Runnable() {
                    @Override
                    public void run() {
                        if(mTextView1!=null)
                            mTextView1.setText(getResources().getString(R.string.charging));
                    }
                });

                mLayout1.setClickable(false);
                mLayout1.setAlpha(0.5f);
                mLayout2.setClickable(true);
                mLayout2.setAlpha(1.0f);
                mLayout3.setClickable(false);
                mLayout3.setAlpha(0.5f);
                mLayout4.setClickable(false);
                mLayout4.setAlpha(0.5f);

                mMenuViewItemUp.setClickable(false);
                mMenuViewItemUp.setAlpha(0.5f);
                mMenuViewItemDown.setClickable(false);
                mMenuViewItemDown.setAlpha(0.5f);
                mMenuViewItemLeft.setClickable(false);
                mMenuViewItemLeft.setAlpha(0.5f);
                mMenuViewItemRight.setClickable(false);
                mMenuViewItemRight.setAlpha(0.5f);
                mMenuViewItemCenter.setClickable(false);
                mMenuViewItemCenter.setAlpha(0.5f);
                break;
            case "":
                getActivity().runOnUiThread(new Runnable() {
                    @Override
                    public void run() {
                        if(mTextView1!=null)
                            mTextView1.setText(getResources().getString(R.string.idle));
                    }
                });

                mLayout1.setClickable(true);
                mLayout1.setAlpha(1.0f);
                mLayout2.setClickable(true);
                mLayout2.setAlpha(1.0f);
                mLayout3.setClickable(false);
                mLayout3.setAlpha(0.5f);
                mLayout4.setClickable(false);
                mLayout4.setAlpha(0.5f);

                mMenuViewItemUp.setClickable(false);
                mMenuViewItemUp.setAlpha(0.5f);
                mMenuViewItemDown.setClickable(false);
                mMenuViewItemDown.setAlpha(0.5f);
                mMenuViewItemLeft.setClickable(false);
                mMenuViewItemLeft.setAlpha(0.5f);
                mMenuViewItemRight.setClickable(false);
                mMenuViewItemRight.setAlpha(0.5f);
                mMenuViewItemCenter.setClickable(false);
                mMenuViewItemCenter.setAlpha(0.5f);
                break;
        }
    }

    public void setStatusControl() {
        OkHttpUtils.get()
                .url(Api.URL_HEADER + Api.QUERY_CODE_STATUS)
                .addParams("macKey", MAC)
                .addParams("sequence", "FFFF")
                .build()
                .execute(new StringCallback() {
                    @Override
                    public void onError(Call call, Exception e, int id) {

                    }

                    @Override
                    public void onResponse(String response, int id) {
                        if (!TextUtils.isEmpty(response)) {
                            try {
                                JSONObject jsonObject = new JSONObject(response);
                                boolean success = jsonObject.optBoolean("success");
                                if (success) {
                                    JSONObject data = jsonObject.optJSONObject("data");
                                    if (data != null) {
                                        String memo = data.optString("memo");
                                        String seq = memo.substring(memo.length() - 10, memo.length() - 6);
                                        LoggerUtils.i("***********code_seq", seq);
                                        String code = memo.substring(memo.length() - 6, memo.length());
                                        LoggerUtils.i("*********code", code);
                                        if (seq.equals("FFFF")) {
                                            controlStatus(code);
                                        }
                                    } else {
                                        controlStatus("");
                                    }
                                }
                            } catch (Exception e) {
                                e.printStackTrace();
                            }
                        }
                    }
                });
    }

/*    @Override
    public void onClick(View v) {
        LoadingDailog.Builder loadBuilder = new LoadingDailog.Builder(mContext)
                .setMessage(getResources().getString(R.string.excuting))
                .setCancelable(true)
                .setCancelOutside(true);
        switch (v.getId()) {
            case R.id.ll_machine_work_1:
                new Thread(new Runnable() {
                    @Override
                    public void run() {
                        sequenceNum = mPreferences.getInt("sequenceNum", 1);
                        String seq = getSeq(Integer.toHexString(sequenceNum));
                        String startPwd = mPreferences.getString("startPwd", "FFFFFFFF");
                        TCPUtils.TCP_sendMsg(Api.SERVER_IP, Api.SERVER_PORT, StringUtils.hexStringToBytes(Api.HEAD + MAC + Api.MY_MAC + startPwd + seq + Api.AUTO));
                    }
                }).start();
                final LoadingDailog dialog1 = loadBuilder.create();
                dialog1.setCancelable(false);   //是否能取消弹窗
                dialog1.show();
                final CountDownTimer countDownTimer1 = new CountDownTimer(5 * 1000, 1000) {
                    @Override
                    public void onTick(long millisUntilFinished) {
                        final String seq = getSeq(Integer.toHexString(mPreferences.getInt("sequenceNum", 1)));
                        OkHttpUtils.get()
                                .url(Api.URL_HEADER + Api.QUERY_CODE_STATUS)
                                .addParams("macKey", MAC)
                                .addParams("sequence", seq)
                                .build()
                                .execute(
                                        new StringCallback() {
                                            @Override
                                            public void onError(Call call, Exception e, int id) {
                                                ToastUtils.showToast(mContext, getResources().getString(R.string.net_fault));
                                            }

                                            @Override
                                            public void onResponse(String response, int id) {
                                                LoggerUtils.i("*********==", response);
                                                if (!TextUtils.isEmpty(response)) {
                                                    try {
                                                        JSONObject jsonObject = new JSONObject(response);
                                                        boolean success = jsonObject.optBoolean("success");
                                                        if (success) {
                                                            JSONObject data = jsonObject.optJSONObject("data");
                                                            if (data != null) {
                                                                String memo = data.optString("memo");
                                                                String sequence = data.optString("sequence");
                                                                if (!TextUtils.isEmpty(memo) && seq.toUpperCase().equals(sequence)) {
                                                                    if (memo.substring(memo.length() - 2, memo.length()).equals("01")) {
                                                                        if ((sequenceNum + 1) > 65534) {
                                                                            sequenceNum = 0;
                                                                            mEditor.putInt("sequenceNum", sequenceNum);
                                                                            mEditor.commit();
                                                                        } else {
                                                                            mEditor.putInt("sequenceNum", sequenceNum + 1);
                                                                            mEditor.commit();
                                                                        }
                                                                        dialog1.dismiss();
                                                                        cancel();
                                                                        ToastUtils.showToast(mContext, getResources().getString(R.string.control_success));
                                                                    } else if (memo.substring(memo.length() - 2, memo.length()).equals("00")) {
                                                                        if ((sequenceNum + 1) > 65534) {
                                                                            sequenceNum = 0;
                                                                            mEditor.putInt("sequenceNum", sequenceNum);
                                                                            mEditor.commit();
                                                                        } else {
                                                                            mEditor.putInt("sequenceNum", sequenceNum + 1);
                                                                            mEditor.commit();
                                                                        }
                                                                        dialog1.dismiss();
                                                                        cancel();
                                                                        ToastUtils.showToast(mContext, getResources().getString(R.string.control_failed));
                                                                    }
                                                                }
                                                            } else {

                                                            }
                                                        }
                                                    } catch (Exception e) {
                                                        e.printStackTrace();
                                                    }
                                                }
                                            }
                                        }
                                );
                    }

                    @Override
                    public void onFinish() {
                        dialog1.dismiss();
                        if ((sequenceNum + 1) > 65534) {
                            sequenceNum = 0;
                            mEditor.putInt("sequenceNum", sequenceNum);
                            mEditor.commit();
                        } else {
                            mEditor.putInt("sequenceNum", sequenceNum + 1);
                            mEditor.commit();
                        }
                        ToastUtils.showToast(mContext, getResources().getString(R.string.no_response));
                    }
                };
                countDownTimer1.start();
                break;
            case R.id.ll_machine_work_2:
                Intent intent = new Intent();
                intent.setClass(mContext, AppointTimeActivity.class);
                mContext.startActivity(intent);
                break;
            case R.id.ll_machine_work_3:
                new Thread(new Runnable() {
                    @Override
                    public void run() {
                        sequenceNum = mPreferences.getInt("sequenceNum", 1);
                        String seq = getSeq(Integer.toHexString(sequenceNum));
                        String startPwd = mPreferences.getString("startPwd", "FFFFFFFF");
                        TCPUtils.TCP_sendMsg(Api.SERVER_IP, Api.SERVER_PORT, StringUtils.hexStringToBytes(Api.HEAD + MAC + Api.MY_MAC + startPwd + seq + Api.CHARGE));
                    }
                }).start();
                final LoadingDailog dialog2 = loadBuilder.create();
                dialog2.setCancelable(false);   //是否能取消弹窗
                dialog2.show();
                final CountDownTimer countDownTimer2 = new CountDownTimer(5 * 1000, 1000) {
                    @Override
                    public void onTick(long millisUntilFinished) {
                        final String seq = getSeq(Integer.toHexString(mPreferences.getInt("sequenceNum", 1)));
                        OkHttpUtils.get()
                                .url(Api.URL_HEADER + Api.QUERY_CODE_STATUS)
                                .addParams("macKey", MAC)
                                .addParams("sequence", seq)
                                .build()
                                .execute(
                                        new StringCallback() {
                                            @Override
                                            public void onError(Call call, Exception e, int id) {
                                                ToastUtils.showToast(mContext, getResources().getString(R.string.net_fault));
                                            }

                                            @Override
                                            public void onResponse(String response, int id) {
                                                if (!TextUtils.isEmpty(response)) {
                                                    try {
                                                        JSONObject jsonObject = new JSONObject(response);
                                                        boolean success = jsonObject.optBoolean("success");
                                                        if (success) {
                                                            JSONObject data = jsonObject.optJSONObject("data");
                                                            if (data != null) {
                                                                String memo = data.optString("memo");
                                                                String sequence = data.optString("sequence");
                                                                if (!TextUtils.isEmpty(memo) && seq.toUpperCase().equals(sequence)) {
                                                                    if (memo.substring(memo.length() - 2, memo.length()).equals("01")) {
                                                                        if ((sequenceNum + 1) > 65534) {
                                                                            sequenceNum = 0;
                                                                            mEditor.putInt("sequenceNum", sequenceNum);
                                                                            mEditor.commit();
                                                                        } else {
                                                                            mEditor.putInt("sequenceNum", sequenceNum + 1);
                                                                            mEditor.commit();
                                                                        }
                                                                        dialog2.dismiss();
                                                                        cancel();
                                                                        setStatusControl();
                                                                        ToastUtils.showToast(mContext, getResources().getString(R.string.control_success));
                                                                    } else if (memo.substring(memo.length() - 2, memo.length()).equals("00")) {
                                                                        if ((sequenceNum + 1) > 65534) {
                                                                            sequenceNum = 0;
                                                                            mEditor.putInt("sequenceNum", sequenceNum);
                                                                            mEditor.commit();
                                                                        } else {
                                                                            mEditor.putInt("sequenceNum", sequenceNum + 1);
                                                                            mEditor.commit();
                                                                        }
                                                                        dialog2.dismiss();
                                                                        cancel();
                                                                        setStatusControl();
                                                                        ToastUtils.showToast(mContext, getResources().getString(R.string.control_failed));
                                                                    }
                                                                }
                                                            } else {

                                                            }
                                                        }
                                                    } catch (Exception e) {
                                                        e.printStackTrace();
                                                    }
                                                }
                                            }
                                        }
                                );
                    }

                    @Override
                    public void onFinish() {
                        dialog2.dismiss();
                        if ((sequenceNum + 1) > 65534) {
                            sequenceNum = 0;
                            mEditor.putInt("sequenceNum", sequenceNum);
                            mEditor.commit();
                        } else {
                            mEditor.putInt("sequenceNum", sequenceNum + 1);
                            mEditor.commit();
                        }
                        ToastUtils.showToast(mContext, getResources().getString(R.string.no_response));
                    }
                };
                countDownTimer2.start();
                break;
            case R.id.ll_machine_work_4:
                if (!isInvolute) {
                    new Thread(new Runnable() {
                        @Override
                        public void run() {
                            sequenceNum = mPreferences.getInt("sequenceNum", 1);
                            String seq = getSeq(Integer.toHexString(sequenceNum));
                            String startPwd = mPreferences.getString("startPwd", "FFFFFFFF");
                            TCPUtils.TCP_sendMsg(Api.SERVER_IP, Api.SERVER_PORT, StringUtils.hexStringToBytes(Api.HEAD + MAC + Api.MY_MAC + startPwd + seq + Api.INVOLUTE));
                        }
                    }).start();
                    final LoadingDailog dialog3 = loadBuilder.create();
                    dialog3.setCancelable(false);   //是否能取消弹窗
                    dialog3.show();
                    final CountDownTimer countDownTimer3 = new CountDownTimer(5 * 1000, 1000) {
                        @Override
                        public void onTick(long millisUntilFinished) {
                            final String seq = getSeq(Integer.toHexString(mPreferences.getInt("sequenceNum", 1)));
                            OkHttpUtils.get()
                                    .url(Api.URL_HEADER + Api.QUERY_CODE_STATUS)
                                    .addParams("macKey", MAC)
                                    .addParams("sequence", seq)
                                    .build()
                                    .execute(
                                            new StringCallback() {
                                                @Override
                                                public void onError(Call call, Exception e, int id) {
                                                    ToastUtils.showToast(mContext, getResources().getString(R.string.net_fault));
                                                }

                                                @Override
                                                public void onResponse(String response, int id) {
                                                    if (!TextUtils.isEmpty(response)) {
                                                        try {
                                                            JSONObject jsonObject = new JSONObject(response);
                                                            boolean success = jsonObject.optBoolean("success");
                                                            if (success) {
                                                                JSONObject data = jsonObject.optJSONObject("data");
                                                                if (data != null) {
                                                                    String memo = data.optString("memo");
                                                                    String sequence = data.optString("sequence");
                                                                    if (!TextUtils.isEmpty(memo) && seq.toUpperCase().equals(sequence)) {
                                                                        if (memo.substring(memo.length() - 2, memo.length()).equals("01")) {
                                                                            if ((sequenceNum + 1) > 65534) {
                                                                                sequenceNum = 0;
                                                                                mEditor.putInt("sequenceNum", sequenceNum);
                                                                                mEditor.commit();
                                                                            } else {
                                                                                mEditor.putInt("sequenceNum", sequenceNum + 1);
                                                                                mEditor.commit();
                                                                            }
                                                                            dialog3.dismiss();
                                                                            cancel();
                                                                            setStatusControl();
//                                                                            isInvolute = true;
                                                                            ToastUtils.showToast(mContext, getResources().getString(R.string.control_success));
                                                                        } else if (memo.substring(memo.length() - 2, memo.length()).equals("00")) {
                                                                            if ((sequenceNum + 1) > 65534) {
                                                                                sequenceNum = 0;
                                                                                mEditor.putInt("sequenceNum", sequenceNum);
                                                                                mEditor.commit();
                                                                            } else {
                                                                                mEditor.putInt("sequenceNum", sequenceNum + 1);
                                                                                mEditor.commit();
                                                                            }
                                                                            dialog3.dismiss();
                                                                            cancel();
                                                                            ToastUtils.showToast(mContext, getResources().getString(R.string.control_failed));
                                                                        }
                                                                    }
                                                                } else {

                                                                }
                                                            }
                                                        } catch (Exception e) {
                                                            e.printStackTrace();
                                                        }
                                                    }
                                                }
                                            }
                                    );
                        }

                        @Override
                        public void onFinish() {
                            dialog3.dismiss();
                            if ((sequenceNum + 1) > 65534) {
                                sequenceNum = 0;
                                mEditor.putInt("sequenceNum", sequenceNum);
                                mEditor.commit();
                            } else {
                                mEditor.putInt("sequenceNum", sequenceNum + 1);
                                mEditor.commit();
                            }
                            ToastUtils.showToast(mContext, getResources().getString(R.string.no_response));
                        }
                    };
                    countDownTimer3.start();
                } else {
                    new Thread(new Runnable() {
                        @Override
                        public void run() {
                            sequenceNum = mPreferences.getInt("sequenceNum", 1);
                            String seq = getSeq(Integer.toHexString(sequenceNum));
                            String startPwd = mPreferences.getString("startPwd", "FFFFFFFF");
                            TCPUtils.TCP_sendMsg(Api.SERVER_IP, Api.SERVER_PORT, StringUtils.hexStringToBytes(Api.HEAD + MAC + Api.MY_MAC + startPwd + seq + Api.INVOLUTE_STOP));
                        }
                    }).start();
                    final LoadingDailog dialog3 = loadBuilder.create();
                    dialog3.setCancelable(false);   //是否能取消弹窗
                    dialog3.show();
                    final CountDownTimer countDownTimer3 = new CountDownTimer(5 * 1000, 1000) {
                        @Override
                        public void onTick(long millisUntilFinished) {
                            final String seq = getSeq(Integer.toHexString(mPreferences.getInt("sequenceNum", 1)));
                            OkHttpUtils.get()
                                    .url(Api.URL_HEADER + Api.QUERY_CODE_STATUS)
                                    .addParams("macKey", MAC)
                                    .addParams("sequence", seq)
                                    .build()
                                    .execute(
                                            new StringCallback() {
                                                @Override
                                                public void onError(Call call, Exception e, int id) {
                                                    ToastUtils.showToast(mContext, getResources().getString(R.string.net_fault));
                                                }

                                                @Override
                                                public void onResponse(String response, int id) {
                                                    if (!TextUtils.isEmpty(response)) {
                                                        try {
                                                            JSONObject jsonObject = new JSONObject(response);
                                                            boolean success = jsonObject.optBoolean("success");
                                                            if (success) {
                                                                JSONObject data = jsonObject.optJSONObject("data");
                                                                if (data != null) {
                                                                    String memo = data.optString("memo");
                                                                    String sequence = data.optString("sequence");
                                                                    if (!TextUtils.isEmpty(memo) && seq.toUpperCase().equals(sequence)) {
                                                                        if (memo.substring(memo.length() - 2, memo.length()).equals("01")) {
                                                                            if ((sequenceNum + 1) > 65534) {
                                                                                sequenceNum = 0;
                                                                                mEditor.putInt("sequenceNum", sequenceNum);
                                                                                mEditor.commit();
                                                                            } else {
                                                                                mEditor.putInt("sequenceNum", sequenceNum + 1);
                                                                                mEditor.commit();
                                                                            }
                                                                            dialog3.dismiss();
                                                                            cancel();
                                                                            setStatusControl();
//                                                                            isInvolute = false;
                                                                            ToastUtils.showToast(mContext, getResources().getString(R.string.control_success));
                                                                        } else if (memo.substring(memo.length() - 2, memo.length()).equals("00")) {
                                                                            if ((sequenceNum + 1) > 65534) {
                                                                                sequenceNum = 0;
                                                                                mEditor.putInt("sequenceNum", sequenceNum);
                                                                                mEditor.commit();
                                                                            } else {
                                                                                mEditor.putInt("sequenceNum", sequenceNum + 1);
                                                                                mEditor.commit();
                                                                            }
                                                                            dialog3.dismiss();
                                                                            cancel();
                                                                            ToastUtils.showToast(mContext, getResources().getString(R.string.control_failed));
                                                                        }
                                                                    }
                                                                } else {

                                                                }
                                                            }
                                                        } catch (Exception e) {
                                                            e.printStackTrace();
                                                        }
                                                    }
                                                }
                                            }
                                    );
                        }

                        @Override
                        public void onFinish() {
                            dialog3.dismiss();
                            if ((sequenceNum + 1) > 65534) {
                                sequenceNum = 0;
                                mEditor.putInt("sequenceNum", sequenceNum);
                                mEditor.commit();
                            } else {
                                mEditor.putInt("sequenceNum", sequenceNum + 1);
                                mEditor.commit();
                            }
                            ToastUtils.showToast(mContext, getResources().getString(R.string.no_response));
                        }
                    };
                    countDownTimer3.start();
                }
                break;
            case R.id.machine_btn_forward:
                new Thread(new Runnable() {
                    @Override
                    public void run() {
                        sequenceNum = mPreferences.getInt("sequenceNum", 1);
                        String seq = getSeq(Integer.toHexString(sequenceNum));
                        String startPwd = mPreferences.getString("startPwd", "FFFFFFFF");
                        TCPUtils.TCP_sendMsg(Api.SERVER_IP, Api.SERVER_PORT, StringUtils.hexStringToBytes(Api.HEAD + MAC+ Api.MY_MAC + startPwd + seq + Api.FORWARD));
                    }
                }).start();
                final LoadingDailog dialog4 = loadBuilder.create();
                dialog4.setCancelable(false);   //是否能取消弹窗
                dialog4.show();
                final CountDownTimer countDownTimer4 = new CountDownTimer(5 * 1000, 1000) {
                    @Override
                    public void onTick(long millisUntilFinished) {
                        final String seq = getSeq(Integer.toHexString(mPreferences.getInt("sequenceNum", 1)));
                        OkHttpUtils.get()
                                .url(Api.URL_HEADER + Api.QUERY_CODE_STATUS)
                                .addParams("macKey", MAC)
                                .addParams("sequence", seq)
                                .build()
                                .execute(
                                        new StringCallback() {
                                            @Override
                                            public void onError(Call call, Exception e, int id) {
                                                ToastUtils.showToast(mContext, getResources().getString(R.string.net_fault));
                                            }

                                            @Override
                                            public void onResponse(String response, int id) {
                                                if (!TextUtils.isEmpty(response)) {
                                                    try {
                                                        JSONObject jsonObject = new JSONObject(response);
                                                        boolean success = jsonObject.optBoolean("success");
                                                        if (success) {
                                                            JSONObject data = jsonObject.optJSONObject("data");
                                                            if (data != null) {
                                                                String memo = data.optString("memo");
                                                                String sequence = data.optString("sequence");
                                                                if (!TextUtils.isEmpty(memo) && seq.toUpperCase().equals(sequence)) {
                                                                    if (memo.substring(memo.length() - 2, memo.length()).equals("01")) {
                                                                        if ((sequenceNum + 1) > 65534) {
                                                                            sequenceNum = 0;
                                                                            mEditor.putInt("sequenceNum", sequenceNum);
                                                                            mEditor.commit();
                                                                        } else {
                                                                            mEditor.putInt("sequenceNum", sequenceNum + 1);
                                                                            mEditor.commit();
                                                                        }
                                                                        setStatusControl();
                                                                        dialog4.dismiss();
                                                                        cancel();
                                                                        ToastUtils.showToast(mContext, getResources().getString(R.string.control_success));
                                                                    } else if (memo.substring(memo.length() - 2, memo.length()).equals("00")) {
                                                                        if ((sequenceNum + 1) > 65534) {
                                                                            sequenceNum = 0;
                                                                            mEditor.putInt("sequenceNum", sequenceNum);
                                                                            mEditor.commit();
                                                                        } else {
                                                                            mEditor.putInt("sequenceNum", sequenceNum + 1);
                                                                            mEditor.commit();
                                                                        }
                                                                        dialog4.dismiss();
                                                                        cancel();
                                                                        ToastUtils.showToast(mContext, getResources().getString(R.string.control_failed));
                                                                    }
                                                                }
                                                            } else {

                                                            }
                                                        }
                                                    } catch (Exception e) {
                                                        e.printStackTrace();
                                                    }
                                                }
                                            }
                                        }
                                );
                    }

                    @Override
                    public void onFinish() {
                        dialog4.dismiss();
                        if ((sequenceNum + 1) > 65534) {
                            sequenceNum = 0;
                            mEditor.putInt("sequenceNum", sequenceNum);
                            mEditor.commit();
                        } else {
                            mEditor.putInt("sequenceNum", sequenceNum + 1);
                            mEditor.commit();
                        }
                        ToastUtils.showToast(mContext, getResources().getString(R.string.no_response));
                    }
                };
                countDownTimer4.start();
                break;
            case R.id.machine_btn_backward:
                new Thread(new Runnable() {
                    @Override
                    public void run() {
                        sequenceNum = mPreferences.getInt("sequenceNum", 1);
                        String seq = getSeq(Integer.toHexString(sequenceNum));
                        String startPwd = mPreferences.getString("startPwd", "FFFFFFFF");
                        TCPUtils.TCP_sendMsg(Api.SERVER_IP, Api.SERVER_PORT, StringUtils.hexStringToBytes(Api.HEAD + MAC+ Api.MY_MAC + startPwd + seq + Api.BACKWARD));
                    }
                }).start();
                final LoadingDailog dialog5 = loadBuilder.create();
                dialog5.setCancelable(false);   //是否能取消弹窗
                dialog5.show();
                final CountDownTimer countDownTimer5 = new CountDownTimer(5 * 1000, 1000) {
                    @Override
                    public void onTick(long millisUntilFinished) {
                        final String seq = getSeq(Integer.toHexString(mPreferences.getInt("sequenceNum", 1)));
                        //                        LoggerUtils.i("**************", seq);
                        OkHttpUtils.get()
                                .url(Api.URL_HEADER + Api.QUERY_CODE_STATUS)
                                .addParams("macKey", MAC)
                                .addParams("sequence", seq)
                                .build()
                                .execute(
                                        new StringCallback() {
                                            @Override
                                            public void onError(Call call, Exception e, int id) {
                                                ToastUtils.showToast(mContext, getResources().getString(R.string.net_fault));
                                            }

                                            @Override
                                            public void onResponse(String response, int id) {
                                                if (!TextUtils.isEmpty(response)) {
                                                    try {
                                                        JSONObject jsonObject = new JSONObject(response);
                                                        boolean success = jsonObject.optBoolean("success");
                                                        if (success) {
                                                            JSONObject data = jsonObject.optJSONObject("data");
                                                            if (data != null) {
                                                                String memo = data.optString("memo");
                                                                String sequence = data.optString("sequence");
                                                                if (!TextUtils.isEmpty(memo) && seq.toUpperCase().equals(sequence)) {
                                                                    if (memo.substring(memo.length() - 2, memo.length()).equals("01")) {
                                                                        if ((sequenceNum + 1) > 65534) {
                                                                            sequenceNum = 0;
                                                                            mEditor.putInt("sequenceNum", sequenceNum);
                                                                            mEditor.commit();
                                                                        } else {
                                                                            mEditor.putInt("sequenceNum", sequenceNum + 1);
                                                                            mEditor.commit();
                                                                        }
                                                                        dialog5.dismiss();
                                                                        cancel();
                                                                        setStatusControl();
                                                                        ToastUtils.showToast(mContext, getResources().getString(R.string.control_success));
                                                                    } else if (memo.substring(memo.length() - 2, memo.length()).equals("00")) {
                                                                        if ((sequenceNum + 1) > 65534) {
                                                                            sequenceNum = 0;
                                                                            mEditor.putInt("sequenceNum", sequenceNum);
                                                                            mEditor.commit();
                                                                        } else {
                                                                            mEditor.putInt("sequenceNum", sequenceNum + 1);
                                                                            mEditor.commit();
                                                                        }
                                                                        dialog5.dismiss();
                                                                        cancel();
                                                                        ToastUtils.showToast(mContext, getResources().getString(R.string.control_failed));
                                                                    }
                                                                }
                                                            } else {

                                                            }
                                                        }
                                                    } catch (Exception e) {
                                                        e.printStackTrace();
                                                    }
                                                }
                                            }
                                        }
                                );
                    }

                    @Override
                    public void onFinish() {
                        dialog5.dismiss();
                        if ((sequenceNum + 1) > 65534) {
                            sequenceNum = 0;
                            mEditor.putInt("sequenceNum", sequenceNum);
                            mEditor.commit();
                        } else {
                            mEditor.putInt("sequenceNum", sequenceNum + 1);
                            mEditor.commit();
                        }
                        ToastUtils.showToast(mContext, getResources().getString(R.string.no_response));
                    }
                };
                countDownTimer5.start();
                break;
            case R.id.machine_btn_left:
                new Thread(new Runnable() {
                    @Override
                    public void run() {
                        sequenceNum = mPreferences.getInt("sequenceNum", 1);
                        String seq = getSeq(Integer.toHexString(sequenceNum));
                        String startPwd = mPreferences.getString("startPwd", "FFFFFFFF");
                        TCPUtils.TCP_sendMsg(Api.SERVER_IP, Api.SERVER_PORT, StringUtils.hexStringToBytes(Api.HEAD + MAC+ Api.MY_MAC + startPwd + seq + Api.LEFT));
                    }
                }).start();
                final LoadingDailog dialog6 = loadBuilder.create();
                dialog6.setCancelable(false);   //是否能取消弹窗
                dialog6.show();
                final CountDownTimer countDownTimer6 = new CountDownTimer(5 * 1000, 1000) {
                    @Override
                    public void onTick(long millisUntilFinished) {
                        final String seq = getSeq(Integer.toHexString(mPreferences.getInt("sequenceNum", 1)));
                        OkHttpUtils.get()
                                .url(Api.URL_HEADER + Api.QUERY_CODE_STATUS)
                                .addParams("macKey", MAC)
                                .addParams("sequence", seq)
                                .build()
                                .execute(
                                        new StringCallback() {
                                            @Override
                                            public void onError(Call call, Exception e, int id) {
                                                ToastUtils.showToast(mContext, getResources().getString(R.string.net_fault));
                                            }

                                            @Override
                                            public void onResponse(String response, int id) {
                                                if (!TextUtils.isEmpty(response)) {
                                                    try {
                                                        JSONObject jsonObject = new JSONObject(response);
                                                        boolean success = jsonObject.optBoolean("success");
                                                        if (success) {
                                                            JSONObject data = jsonObject.optJSONObject("data");
                                                            if (data != null) {
                                                                String memo = data.optString("memo");
                                                                String sequence = data.optString("sequence");
                                                                if (!TextUtils.isEmpty(memo) && seq.toUpperCase().equals(sequence)) {
                                                                    if (memo.substring(memo.length() - 2, memo.length()).equals("01")) {
                                                                        if ((sequenceNum + 1) > 65534) {
                                                                            sequenceNum = 0;
                                                                            mEditor.putInt("sequenceNum", sequenceNum);
                                                                            mEditor.commit();
                                                                        } else {
                                                                            mEditor.putInt("sequenceNum", sequenceNum + 1);
                                                                            mEditor.commit();
                                                                        }
                                                                        dialog6.dismiss();
                                                                        cancel();
                                                                        setStatusControl();
                                                                        ToastUtils.showToast(mContext, getResources().getString(R.string.control_success));
                                                                    } else if (memo.substring(memo.length() - 2, memo.length()).equals("00")) {
                                                                        if ((sequenceNum + 1) > 65534) {
                                                                            sequenceNum = 0;
                                                                            mEditor.putInt("sequenceNum", sequenceNum);
                                                                            mEditor.commit();
                                                                        } else {
                                                                            mEditor.putInt("sequenceNum", sequenceNum + 1);
                                                                            mEditor.commit();
                                                                        }
                                                                        dialog6.dismiss();
                                                                        cancel();
                                                                        ToastUtils.showToast(mContext, getResources().getString(R.string.control_failed));
                                                                    }
                                                                }
                                                            } else {

                                                            }
                                                        }
                                                    } catch (Exception e) {
                                                        e.printStackTrace();
                                                    }
                                                }
                                            }
                                        }
                                );
                    }

                    @Override
                    public void onFinish() {
                        dialog6.dismiss();
                        if ((sequenceNum + 1) > 65534) {
                            sequenceNum = 0;
                            mEditor.putInt("sequenceNum", sequenceNum);
                            mEditor.commit();
                        } else {
                            mEditor.putInt("sequenceNum", sequenceNum + 1);
                            mEditor.commit();
                        }
                        ToastUtils.showToast(mContext, getResources().getString(R.string.no_response));
                    }
                };
                countDownTimer6.start();
                break;
            case R.id.machine_btn_right:
                new Thread(new Runnable() {
                    @Override
                    public void run() {
                        sequenceNum = mPreferences.getInt("sequenceNum", 1);
                        String seq = getSeq(Integer.toHexString(sequenceNum));
                        String startPwd = mPreferences.getString("startPwd", "FFFFFFFF");
                        TCPUtils.TCP_sendMsg(Api.SERVER_IP, Api.SERVER_PORT, StringUtils.hexStringToBytes(Api.HEAD + MAC+ Api.MY_MAC + startPwd + seq + Api.RIGHT));
                    }
                }).start();
                final LoadingDailog dialog7 = loadBuilder.create();
                dialog7.setCancelable(false);   //是否能取消弹窗
                dialog7.show();
                final CountDownTimer countDownTimer7 = new CountDownTimer(5 * 1000, 1000) {
                    @Override
                    public void onTick(long millisUntilFinished) {
                        final String seq = getSeq(Integer.toHexString(mPreferences.getInt("sequenceNum", 1)));
                        OkHttpUtils.get()
                                .url(Api.URL_HEADER + Api.QUERY_CODE_STATUS)
                                .addParams("macKey", MAC)
                                .addParams("sequence", seq)
                                .build()
                                .execute(
                                        new StringCallback() {
                                            @Override
                                            public void onError(Call call, Exception e, int id) {
                                                ToastUtils.showToast(mContext, getResources().getString(R.string.net_fault));
                                            }

                                            @Override
                                            public void onResponse(String response, int id) {
                                                if (!TextUtils.isEmpty(response)) {
                                                    try {
                                                        JSONObject jsonObject = new JSONObject(response);
                                                        boolean success = jsonObject.optBoolean("success");
                                                        if (success) {
                                                            JSONObject data = jsonObject.optJSONObject("data");
                                                            if (data != null) {
                                                                String memo = data.optString("memo");
                                                                String sequence = data.optString("sequence");
                                                                if (!TextUtils.isEmpty(memo) && seq.toUpperCase().equals(sequence)) {
                                                                    if (memo.substring(memo.length() - 2, memo.length()).equals("01")) {
                                                                        if ((sequenceNum + 1) > 65534) {
                                                                            sequenceNum = 0;
                                                                            mEditor.putInt("sequenceNum", sequenceNum);
                                                                            mEditor.commit();
                                                                        } else {
                                                                            mEditor.putInt("sequenceNum", sequenceNum + 1);
                                                                            mEditor.commit();
                                                                        }
                                                                        dialog7.dismiss();
                                                                        cancel();
                                                                        setStatusControl();
                                                                        ToastUtils.showToast(mContext, getResources().getString(R.string.control_success));
                                                                    } else if (memo.substring(memo.length() - 2, memo.length()).equals("00")) {
                                                                        if ((sequenceNum + 1) > 65534) {
                                                                            sequenceNum = 0;
                                                                            mEditor.putInt("sequenceNum", sequenceNum);
                                                                            mEditor.commit();
                                                                        } else {
                                                                            mEditor.putInt("sequenceNum", sequenceNum + 1);
                                                                            mEditor.commit();
                                                                        }
                                                                        dialog7.dismiss();
                                                                        cancel();
                                                                        ToastUtils.showToast(mContext, getResources().getString(R.string.control_failed));
                                                                    }
                                                                }
                                                            } else {

                                                            }
                                                        }
                                                    } catch (Exception e) {
                                                        e.printStackTrace();
                                                    }
                                                }
                                            }
                                        }
                                );
                    }

                    @Override
                    public void onFinish() {
                        dialog7.dismiss();
                        if ((sequenceNum + 1) > 65534) {
                            sequenceNum = 0;
                            mEditor.putInt("sequenceNum", sequenceNum);
                            mEditor.commit();
                        } else {
                            mEditor.putInt("sequenceNum", sequenceNum + 1);
                            mEditor.commit();
                        }
                        ToastUtils.showToast(mContext, getResources().getString(R.string.no_response));
                    }
                };
                countDownTimer7.start();
                break;
            case R.id.machine_btn_run:
                if (isRunning) {
                    new Thread(new Runnable() {
                        @Override
                        public void run() {
                            sequenceNum = mPreferences.getInt("sequenceNum", 1);
                            String seq = getSeq(Integer.toHexString(sequenceNum));
                            String startPwd = mPreferences.getString("startPwd", "FFFFFFFF");
                            TCPUtils.TCP_sendMsg(Api.SERVER_IP, Api.SERVER_PORT, StringUtils.hexStringToBytes(Api.HEAD + MAC+ Api.MY_MAC + startPwd + seq + Api.START));
                        }
                    }).start();
                    final Timer mTimer8 = new Timer();
                    final LoadingDailog dialog8 = loadBuilder.create();
                    dialog8.setCancelable(false);   //是否能取消弹窗
                    dialog8.show();
                    final CountDownTimer countDownTimer8 = new CountDownTimer(5 * 1000, 1000) {
                        @Override
                        public void onTick(long millisUntilFinished) {
                            final String seq = getSeq(Integer.toHexString(mPreferences.getInt("sequenceNum", 1)));
                            OkHttpUtils.get()
                                    .url(Api.URL_HEADER + Api.QUERY_CODE_STATUS)
                                    .addParams("macKey", MAC)
                                    .addParams("sequence", seq)
                                    .build()
                                    .execute(
                                            new StringCallback() {
                                                @Override
                                                public void onError(Call call, Exception e, int id) {
                                                    ToastUtils.showToast(mContext, getResources().getString(R.string.net_fault));
                                                }

                                                @Override
                                                public void onResponse(String response, int id) {
                                                    if (!TextUtils.isEmpty(response)) {
                                                        try {
                                                            JSONObject jsonObject = new JSONObject(response);
                                                            boolean success = jsonObject.optBoolean("success");
                                                            if (success) {
                                                                JSONObject data = jsonObject.optJSONObject("data");
                                                                if (data != null) {
                                                                    String memo = data.optString("memo");
                                                                    String sequence = data.optString("sequence");
                                                                    if (!TextUtils.isEmpty(memo) && seq.toUpperCase().equals(sequence)) {
                                                                        if (memo.substring(memo.length() - 2, memo.length()).equals("01")) {
                                                                            if ((sequenceNum + 1) > 65534) {
                                                                                sequenceNum = 0;
                                                                                mEditor.putInt("sequenceNum", sequenceNum);
                                                                                mEditor.commit();
                                                                            } else {
                                                                                mEditor.putInt("sequenceNum", sequenceNum + 1);
                                                                                mEditor.commit();
                                                                            }
                                                                            dialog8.dismiss();
                                                                            cancel();
                                                                            isRunning = false;
                                                                            mMenuViewItemCenter.setBackgroundResource(R.mipmap.machine_btn_run);
                                                                            setStatusControl();
                                                                            ToastUtils.showToast(mContext, getResources().getString(R.string.control_success));
                                                                        } else if (memo.substring(memo.length() - 2, memo.length()).equals("00")) {
                                                                            if ((sequenceNum + 1) > 65534) {
                                                                                sequenceNum = 0;
                                                                                mEditor.putInt("sequenceNum", sequenceNum);
                                                                                mEditor.commit();
                                                                            } else {
                                                                                mEditor.putInt("sequenceNum", sequenceNum + 1);
                                                                                mEditor.commit();
                                                                            }
                                                                            dialog8.dismiss();
                                                                            cancel();
                                                                            ToastUtils.showToast(mContext, getResources().getString(R.string.control_failed));
                                                                        }
                                                                    }
                                                                } else {

                                                                }
                                                            }
                                                        } catch (Exception e) {
                                                            e.printStackTrace();
                                                        }
                                                    }
                                                }
                                            }
                                    );
                        }

                        @Override
                        public void onFinish() {
                            dialog8.dismiss();
                            if ((sequenceNum + 1) > 65534) {
                                sequenceNum = 0;
                                mEditor.putInt("sequenceNum", sequenceNum);
                                mEditor.commit();
                            } else {
                                mEditor.putInt("sequenceNum", sequenceNum + 1);
                                mEditor.commit();
                            }
                            ToastUtils.showToast(mContext, getResources().getString(R.string.no_response));
                        }
                    };
                    countDownTimer8.start();
                } else {
                    new Thread(new Runnable() {
                        @Override
                        public void run() {
                            sequenceNum = mPreferences.getInt("sequenceNum", 1);
                            String seq = getSeq(Integer.toHexString(sequenceNum));
                            String startPwd = mPreferences.getString("startPwd", "FFFFFFFF");
                            TCPUtils.TCP_sendMsg(Api.SERVER_IP, Api.SERVER_PORT, StringUtils.hexStringToBytes(Api.HEAD + MAC+ Api.MY_MAC + startPwd + seq + Api.STOP));
                        }
                    }).start();
                    final Timer mTimer9 = new Timer();
                    final LoadingDailog dialog9 = loadBuilder.create();
                    dialog9.setCancelable(false);   //是否能取消弹窗
                    dialog9.show();
                    final CountDownTimer countDownTimer9 = new CountDownTimer(5 * 1000, 1000) {
                        @Override
                        public void onTick(long millisUntilFinished) {
                            final String seq = getSeq(Integer.toHexString(mPreferences.getInt("sequenceNum", 1)));
                            OkHttpUtils.get()
                                    .url(Api.URL_HEADER + Api.QUERY_CODE_STATUS)
                                    .addParams("macKey", MAC)
                                    .addParams("sequence", seq)
                                    .build()
                                    .execute(
                                            new StringCallback() {
                                                @Override
                                                public void onError(Call call, Exception e, int id) {
                                                    ToastUtils.showToast(mContext, getResources().getString(R.string.net_fault));
                                                }

                                                @Override
                                                public void onResponse(String response, int id) {
                                                    if (!TextUtils.isEmpty(response)) {
                                                        try {
                                                            JSONObject jsonObject = new JSONObject(response);
                                                            boolean success = jsonObject.optBoolean("success");
                                                            if (success) {
                                                                JSONObject data = jsonObject.optJSONObject("data");
                                                                if (data != null) {
                                                                    String memo = data.optString("memo");
                                                                    String sequence = data.optString("sequence");
                                                                    if (!TextUtils.isEmpty(memo) && seq.toUpperCase().equals(sequence)) {
                                                                        if (memo.substring(memo.length() - 2, memo.length()).equals("01")) {
                                                                            if ((sequenceNum + 1) > 65534) {
                                                                                sequenceNum = 0;
                                                                                mEditor.putInt("sequenceNum", sequenceNum);
                                                                                mEditor.commit();
                                                                            } else {
                                                                                mEditor.putInt("sequenceNum", sequenceNum + 1);
                                                                                mEditor.commit();
                                                                            }
                                                                            dialog9.dismiss();
                                                                            cancel();
                                                                            isRunning = true;
                                                                            mMenuViewItemCenter.setBackgroundResource(R.mipmap.machine_btn_pause);
                                                                            setStatusControl();
                                                                            ToastUtils.showToast(mContext, getResources().getString(R.string.control_success));
                                                                        } else if (memo.substring(memo.length() - 2, memo.length()).equals("00")) {
                                                                            if ((sequenceNum + 1) > 65534) {
                                                                                sequenceNum = 0;
                                                                                mEditor.putInt("sequenceNum", sequenceNum);
                                                                                mEditor.commit();
                                                                            } else {
                                                                                mEditor.putInt("sequenceNum", sequenceNum + 1);
                                                                                mEditor.commit();
                                                                            }
                                                                            dialog9.dismiss();
                                                                            cancel();
                                                                            ToastUtils.showToast(mContext, getResources().getString(R.string.control_failed));
                                                                        }
                                                                    }
                                                                } else {

                                                                }
                                                            }
                                                        } catch (Exception e) {
                                                            e.printStackTrace();
                                                        }
                                                    }
                                                }
                                            }
                                    );
                        }

                        @Override
                        public void onFinish() {
                            dialog9.dismiss();
                            if ((sequenceNum + 1) > 65534) {
                                sequenceNum = 0;
                                mEditor.putInt("sequenceNum", sequenceNum);
                                mEditor.commit();
                            } else {
                                mEditor.putInt("sequenceNum", sequenceNum + 1);
                                mEditor.commit();
                            }
                            ToastUtils.showToast(mContext, getResources().getString(R.string.no_response));
                        }
                    };
                    countDownTimer9.start();
                }
                break;
        }
    }*/

}
